package ReSofting.it.GoldDirector.Helpers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import ReSofting.it.GoldDirector.R;
import ReSofting.it.GoldDirector.R.menu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public  class resVarie {
	
	
	public static String myNameSpace = "ReSofting.it.GoldDirector";
	
	public static Intent orderItemsView = new Intent().setClassName( myNameSpace,  myNameSpace + ".OrderItemsActivity");//namespace + nome classe
	public static Intent settingsView = new Intent().setClassName(myNameSpace ,myNameSpace + ".SettingsActivity");
	
	
	public static String SERVICE_URI = "192.168.1.2:8096/Services/OrdersSvc.svc/";
	
	
	public static String txtMission = 		"";

	
	public static void showMessageDialog(String pTxt,Context pContext)
	{
		
		AlertDialog.Builder builder = new AlertDialog.Builder(pContext);
		builder.setMessage(pTxt)
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.cancel();  //MyActivity.this.finish();
		           }
		       });
		       
		AlertDialog alert = builder.create();
		alert.show(); 
		
	}//fM
	
	public static void Alert2(String pTitle, String pTxt,Context pContext)
	{
		new AlertDialog.Builder(pContext)

		.setTitle(pTitle)

		.setMessage(pTxt)

		.setNeutralButton("Ok",

		new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog,

		int which) {	dialog.cancel();	}

		}).show();
	}//fM
	
	
	//Menu handler
	public static Boolean handlerMenu(MenuItem item,Context pContext)
	{
		//resVarie.showMessageDialog((String) item.getTitle(), pContext);
		String titolo = (String) item.getTitle();
		
		if(titolo.equalsIgnoreCase("Settings"))
			pContext.startActivity(resVarie.settingsView);  
	        
		if(titolo.equalsIgnoreCase("Ordini"))
			pContext.startActivity(resVarie.orderItemsView); 		

	   return true;
		 
		    
	}//fM
	
	// menu inflater
	public static  boolean onCreateOptionsMenu(Menu menu,Activity pActiv) {
		
		 MenuInflater inflater = pActiv.getMenuInflater();
	     inflater.inflate(R.menu.mainmenu, menu); //<<< nome del menu
	     return true ;

	}//fM
	
	
	public static <T> T findInList (List<T> pList, T pObject)
	{
		 for (int i = 0; i < pList.size(); ++i) 
	        {	        					
	        	if (  pList.get(i).equals(pObject)  )  
	        		return pObject;			
	        }   
		 
		 return null;
		
	}//fM
	
		
	public static JSONObject sendJsonRequest(String host, int port,
		       String uri,JSONStringer  param) //JSONObject
		            throws ClientProtocolException, IOException, JSONException
		{
		    HttpClient httpClient = new DefaultHttpClient();
		    HttpHost httpHost = new HttpHost(host, port);   
		    HttpPost httpPost = new HttpPost(uri);
		    httpPost.addHeader("Content-Type", "application/json; charset=utf-8");


		if (param != null)
		{
		    HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8");
		    httpPost.setEntity(bodyEntity);
		}


		HttpResponse response = httpClient.execute(httpHost, httpPost);
		HttpEntity entity = response.getEntity();


		String result = null;
		if (entity != null) {
		    InputStream instream = entity.getContent();
		    BufferedReader reader = new BufferedReader(
		         new InputStreamReader(instream));
		    StringBuilder sb = new StringBuilder();


		    String line = null;
		    while ((line = reader.readLine()) != null)
		        sb.append(line + "\n");


		    result = sb.toString();
		    instream.close();           
		}


		httpPost.abort();
		return result != null ? new JSONObject(result) : null;


		}
	
	public static String readJsonString(HttpEntity entity)
	{

		String result = null;
		if (entity != null) {
		    InputStream instream = null;
		    
			try {
				instream = entity.getContent();
				} catch (Exception e) {
					e.printStackTrace();
			}
				
		    BufferedReader reader = new BufferedReader(
		         new InputStreamReader(instream));
		    StringBuilder sb = new StringBuilder();


		    String line = null;
		    try {
				while ((line = reader.readLine()) != null)
				    sb.append(line + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		    result = sb.toString();
		    try {
		    	instream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}           
			

		}
		return result;
	
	}//fM

	
	 //"%dT%m%Y %H%M%S" 
    public synchronized static Time getCurrentDateTime()
    {
    	Time today = new Time(Time.getCurrentTimezone());
    	today.setToNow();
    	
    	return today;

    	
    }//fM
    
    
    public static boolean isDebuggable(Context ctx)
    {
        boolean debuggable = false;
     
        PackageManager pm = ctx.getPackageManager();
        try
        {
            ApplicationInfo appinfo = pm.getApplicationInfo(ctx.getPackageName(), 0);
            debuggable = (0 != (appinfo.flags &= ApplicationInfo.FLAG_DEBUGGABLE));
        }
        catch(NameNotFoundException e)
        {
            /*debuggable variable will remain false*/
        }
         
        return debuggable;
    }
	
}